package com.kantboot.system.user.balance.dao.repository;

import com.kantboot.system.user.balance.domain.entity.SysUserBalance;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

public interface SysUserBalanceRepository extends JpaRepository<SysUserBalance, Long>{

    /**
     * 变动余额
     */
    @Modifying
    @Transactional
    @Query("""
            UPDATE SysUserBalance
            SET number = number + :number
            WHERE userId = :userId
            AND balanceCode = :balanceCode
            """)
    void changeBalance(
            @Param("userId") Long userId,
            @Param("balanceCode") String balanceCode,
            @Param("number") BigDecimal number);

    /**
     * 根据用户id和余额code查询余额
     * @param userId 用户id
     * @param balanceCode 余额code
     * @return 余额
     */
    SysUserBalance findByUserIdAndBalanceCode(Long userId, String balanceCode);


}
